import { Chat } from "@/types/Chat"


export function groupByDate(chatList: Chat[]) {

  const groupMap = new Map<string, Chat[]>()//存储键值对 键：字符串 值：chat类型的数组
  chatList.forEach((item) => {
    console.log(item)
    const now = new Date()
    const upDateTime = new Date(item.updateTime)
    let key = '未知时间'
    const dayDiff = Math.floor((now.getTime() - upDateTime.getTime()) / (1000 * 60 * 60 * 24))
    if (dayDiff === 0 && now.getDate() === upDateTime.getDate()) {
      key = "今天"
    } else if (dayDiff <= 7) {
      key = "最近7天"
    } else if (dayDiff <= 31) {
      key = "最近一个月"
    } else if (now.getFullYear() === upDateTime.getFullYear()) {
      key = `${upDateTime.getMonth() + 1}月`
    } else {
      key = `${upDateTime.getFullYear()}`
    }
    if (groupMap.has(key)) {
      groupMap.get(key)?.push(item)
    } else {
      groupMap.set(key, [item])
    }
  })
  groupMap.forEach((item) => {
    item.sort((a, b) => b.updateTime - a.updateTime)
  })

  const groupList = Array.from(groupMap).sort(([, list1], [, list2]) => {
    return (
      list2[list2.length - 1].updateTime - list1[list1.length - 1].updateTime
    )
  })
  return groupList
}